From 1669878c08607f481e3f879d6914fc4d3c9d7206 Mon Sep 17 00:00:00 2001
From: Daniel Bylinka <daniel.bylinka@gmail.com>
Date: Fri, 2 Apr 2021 19:20:17 +0200
Subject: [PATCH] [statuscmd] Format status text and handle button signals

---
 dwmblocks.c | 48 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 41 insertions(+), 7 deletions(-)

diff --git a/dwmblocks.c b/dwmblocks.c
index ded717c..78fdeb5 100644
--- a/dwmblocks.c
+++ b/dwmblocks.c
@@ -3,6 +3,7 @@
 #include<string.h>
 #include<unistd.h>
 #include<signal.h>
+#include<sys/wait.h>
 #ifndef NO_X
 #include<X11/Xlib.h>
 #endif
@@ -27,14 +28,14 @@ typedef struct {
 #ifndef __OpenBSD__
 void dummysighandler(int num);
 #endif
-void sighandler(int num);
 void getcmds(int time);
 void getsigcmds(unsigned int signal);
 void setupsignals();
-void sighandler(int signum);
+void sighandler(int signum, siginfo_t *si, void *ucontext);
 int getstatus(char *str, char *last);
 void statusloop();
 void termhandler();
+void chldhandler();
 void pstdout();
 #ifndef NO_X
 void setroot();
@@ -58,6 +59,8 @@ static int returnStatus = 0;
 //opens process *cmd and stores output in *output
 void getcmd(const Block *block, char *output)
 {
+	if (block->signal)
+		*output++ = block->signal;
 	strcpy(output, block->icon);
 	FILE *cmdf = popen(block->command, "r");
 	if (!cmdf)
@@ -102,15 +105,18 @@ void getsigcmds(unsigned int signal)
 
 void setupsignals()
 {
+	struct sigaction sa = { .sa_sigaction = sighandler, .sa_flags = SA_SIGINFO };
 #ifndef __OpenBSD__
 	    /* initialize all real time signals with dummy handler */
-    for (int i = SIGRTMIN; i <= SIGRTMAX; i++)
+    for (int i = SIGRTMIN; i <= SIGRTMAX; i++) {
         signal(i, dummysighandler);
+		sigaddset(&sa.sa_mask, i);
+	}
 #endif
 
 	for (unsigned int i = 0; i < LENGTH(blocks); i++) {
 		if (blocks[i].signal > 0)
-			signal(SIGMINUS+blocks[i].signal, sighandler);
+			sigaction(SIGMINUS+blocks[i].signal, &sa, NULL);
 	}
 
 }
@@ -178,10 +184,32 @@ void dummysighandler(int signum)
 }
 #endif
 
-void sighandler(int signum)
+void sighandler(int signum, siginfo_t *si, void *ucontext)
 {
-	getsigcmds(signum-SIGPLUS);
-	writestatus();
+	if (si->si_value.sival_int) {
+		pid_t parent = getpid();
+		if (fork() == 0) {
+#ifndef NO_X
+			if (dpy)
+				close(ConnectionNumber(dpy));
+#endif
+			int i;
+			for (i = 0; i < LENGTH(blocks) && blocks[i].signal != signum-SIGRTMIN; i++);
+
+			char shcmd[1024];
+			sprintf(shcmd, "%s; kill -%d %d", blocks[i].command, SIGRTMIN+blocks[i].signal, parent);
+			char *cmd[] = { "/bin/sh", "-c", shcmd, NULL };
+			char button[2] = { '0' + si->si_value.sival_int, '\0' };
+			setenv("BUTTON", button, 1);
+			setsid();
+			execvp(cmd[0], cmd);
+			perror(cmd[0]);
+			exit(EXIT_SUCCESS);
+		}
+	} else {
+		getsigcmds(signum-SIGPLUS);
+		writestatus();
+	}
 }
 
 void termhandler()
@@ -189,6 +217,11 @@ void termhandler()
 	statusContinue = 0;
 }
 
+void chldhandler()
+{
+	while (0 < waitpid(-1, NULL, WNOHANG));
+}
+
 int main(int argc, char** argv)
 {
 	for (int i = 0; i < argc; i++) {//Handle command line arguments
@@ -205,6 +238,7 @@ int main(int argc, char** argv)
 	delim[delimLen++] = '\0';
 	signal(SIGTERM, termhandler);
 	signal(SIGINT, termhandler);
+	signal(SIGCHLD, chldhandler);
 	statusloop();
 #ifndef NO_X
 	XCloseDisplay(dpy);
-- 
2.31.0

